function saveSubmit(){

    // 获取输入框
    var name = document.getElementById('nameInput').value;
    var telephone = document.getElementById('telephoneInput').value;
    var age = document.getElementById('ageInput').value;
    var time = document.getElementById('timeInput').value;

    // 创建要保存的数据对象
   

    
   
        //Create
        const appointmentData = {
        name: name,
        telephone: telephone,
        age: age,
        time: time
    }; //JS Object

    //Save to local storage
    addToAppointment(appointmentData);
    location.href = "../person.html";
}



function addToAppointment(appointmentData) {
    try {
        // 获取当前用户
        const user = JSON.parse(localStorage.getItem("loggedinUser"));
        if (!user) {
            alert("Please log in first");
            return;
        }

        // 获取用户列表
        const users = JSON.parse(localStorage.getItem("users")) || [];
        const currentUser = users.find(u => u.email === user.email);

        if (!currentUser) {
            alert("The user does not exist");
            return;
        }

        // 修正：使用正确的属性名（例如 "appointments" 而非 "appointment"）
        if (!currentUser.appointment) {
            currentUser.appointment = []; // 初始化预约数组
        }

        // 添加新预约
        currentUser.appointment.push(appointmentData);

        // 保存更新后的用户数据
        localStorage.setItem("users", JSON.stringify(users));
        alert("The reservation has been successfully saved!！");
    } catch (error) {
        console.error("Local storage failed:", error);
        alert("Save failed. Please try again later");
    }
}




// 辅助函数：安全获取本地存储数据
function getLocalStorage(key) {
    try {
        return JSON.parse(localStorage.getItem(key));
    } catch (error) {
        console.error(`Read the local storage ${key} Failure:`, error);
        return null;
    }
}

// 辅助函数：安全设置本地存储数据
function setLocalStorage(key, data) {
    try {
        localStorage.setItem(key, JSON.stringify(data));
        return true;
    } catch (error) {
        console.error(`Write to local storage ${key} Failure:`, error);
        alert("The local storage is full or disabled");
        return false;
    }
}

// 修改后的 addToAppointment 函数
function addToAppointment(appointmentData) {
    const user = getLocalStorage("loggedinUser");
    if (!user) {
        alert("Please log in first");
        return;
    }

    const users = getLocalStorage("users") || [];
    const currentUser = users.find(u => u.email === user.email);

    if (!currentUser) {
        alert("The user does not exist");
        return;
    }

    // 验证数据
    if (!appointmentData.name || !appointmentData.telephone) {
        alert("The name and phone number cannot be empty");
        return;
    }

    // 添加预约
    if (!currentUser.appointments) currentUser.appointments = [];
    currentUser.appointments.push(appointmentData);

    // 保存更新
    if (setLocalStorage("users", users)) {
        alert("The reservation was successfully saved！");
        // 可添加页面跳转逻辑
    }
}